//
//  CZNewFeatureCell.swift
//  iOSWeibo10
//
//  Created by itcast on 16/5/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZNewFeatureCell: UICollectionViewCell {
    
    // 根据cell显示的哪一页设置对应的图片
    var index: Int = 0 {
        didSet {
            // cell要显示的那一刻,collectionView就会来给cell设置index,让我们的cell知道要显示哪一页
            // 根据页码设置图片
            CZPrint(items: "第\(index)页要显示")
//            bkgImageView.image = UIImage(named: "new_feature_\(index + 1)")
            let file = NSBundle.mainBundle().pathForResource("new_feature_\(index + 1)@2x", ofType: "png")!
            bkgImageView.image = UIImage(contentsOfFile: file)
            
            // 隐藏开始按钮
//            startButton.hidden = true
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    private func prepareUI() {
        // 1.添加控件
        contentView.addSubview(bkgImageView)
        contentView.addSubview(shareButton)
        contentView.addSubview(startButton)
        
        // 2.添加约束
        bkgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.contentView)
        }
        
        shareButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.contentView)
            make.bottom.equalTo(startButton.snp_top).offset(-16)
        }
        
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.contentView)
            make.bottom.equalTo(self.contentView).offset(-160)
        }
    }
    
    /// 开始按钮动画
//    func startAnimation() {
//        // 动画之前,将按钮显示出来
////        startButton.hidden = false
//        // 在动画前设置transform.scale为0.看不到
//        startButton.transform = CGAffineTransformMakeScale(0, 0)
//        
//        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
//            // 恢复到原来的大小
//            self.startButton.transform = CGAffineTransformMakeScale(1, 1) // 清空transform到默认的状态 scale = 1
//            }, completion: nil)
//    }
    
    // MARK: - 按钮点击事件
    /// 分享按钮被点击
    func didClickShareButton() {
        self.shareButton.selected = !self.shareButton.selected
    }
    
    /// 开始按钮被点击
    func didClickStartButton() {
        // 切换到CZMainTabBarController
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.switchRootViewController(CZMainTabBarController())
    }
    
    // MARK: - 懒加载
    /// 背景imageView
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    /// 分享按钮
    lazy var shareButton: UIButton = {
        let button = UIButton()
        
        // 设置文字
        button.setTitle("分享给大家", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        
        button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        
        // 点击事件
        button.addTarget(self, action: "didClickShareButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    /// 开始按钮
    lazy var startButton: UIButton = {
        let button = UIButton()
        
        // 设置文字
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 点击事件
        button.addTarget(self, action: "didClickStartButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
}
